﻿using System;
using System.Collections.Generic;
using Fizzler.Systems.HtmlAgilityPack;
using HtmlAgilityPack;

namespace AutoF3.ProblemRetrievers
{
    public abstract class ArtOfProblemSolvingRetriever : ProblemRetriever
    {
        protected string CleanUpPostHtml(string postText)
        {
            string ret = postText.Replace("<br>", "\r\n");
            ret = ret.Replace("&nbsp;", "");
            ret = ret.Replace("&lt;", "<");
            ret = ret.Replace("&gt;", ">");
            ret = ret.Replace("&amp;", "&");
            ret = ret.Replace("&cent;", "¢");
            ret = ret.Replace("&pound;", "£");
            ret = ret.Replace("&yen;", "¥");
            ret = ret.Replace("&euro;", "€");
            ret = ret.Replace("&sect;", "§");
            ret = ret.Replace("&copy;", "©");
            ret = ret.Replace("&reg;", "®");
            ret = ret.Replace("&trade;", "™");

            return ret;
        }

        protected string GetPostTextInTeXFormat(HtmlNode node)
        {
            var html = new HtmlDocument();
            string post = node.InnerHtml;

            html.LoadHtml(post);
            var document = html.DocumentNode;

            var texNodes = document.QuerySelectorAll("img.latex");
            foreach (var texNode in texNodes)
            {
                string toReplace = texNode.ParentNode.OuterHtml;
                string texStr = texNode.GetAttributeValue("title", null);

                post = post.Replace(toReplace, "$" + texStr + "$");
            }

            html.LoadHtml(post);
            document = html.DocumentNode;

            texNodes = document.QuerySelectorAll("img.latexcenter");
            foreach (var texNode in texNodes)
            {
                string toReplace = texNode.ParentNode.OuterHtml;
                string texStr = texNode.GetAttributeValue("title", null);

                post = post.Replace(toReplace, "$$" + texStr + "$$");
            }

            html.LoadHtml(post);
            document = html.DocumentNode;

            texNodes = document.QuerySelectorAll("span.hidden-label");
            foreach (var texNode in texNodes)
            {
                string toReplace = texNode.OuterHtml;
                string texStr = texNode.InnerHtml + "\r\n";

                post = post.Replace(toReplace, texStr);
            }

            html.LoadHtml(post);
            document = html.DocumentNode;

            texNodes = document.QuerySelectorAll("span");
            foreach (var texNode in texNodes)
            {
                string fontType = texNode.GetAttributeValue("style", null);
                if (fontType != null)
                {
                    fontType = fontType.Replace(" ", "");
                    string toReplace, texStr;

                    if (fontType.Equals("font-style:italic") || fontType.Equals("font-style:oblique")) 
                    {
                        toReplace = texNode.OuterHtml;
                        texStr = "\\emph{" + texNode.InnerHtml + "}";
                    }
                    else 
                    {
                        toReplace = texNode.OuterHtml;
                        texStr = texNode.InnerHtml;
                    }

                    post = post.Replace(toReplace, texStr);
                }
                else
                    Console.WriteLine("new case");
            }

            //hidden sections

            //image stuff goes here

            //take care of any div tags we didn't handle before
            html.LoadHtml(post);
            document = html.DocumentNode;

            texNodes = document.QuerySelectorAll("div");
            foreach (var texNode in texNodes)
            {
                string toReplace = texNode.OuterHtml;
                string texStr = texNode.InnerHtml;

                post = post.Replace(toReplace, texStr);
            }

            return post;
        }
    }
}
